<?php

namespace app\api\command;

use app\common\model\Packages;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Ordersix extends Command
{
    protected function configure()
    {
        $this->setName('ordersix')->setHelp('平台6订单查询')->setDescription('平台6订单查询');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel = new \app\common\model\Order();
        $order      = $orderModel->where('platform_type', 6)->whereIn('order_status',
            '-1,1,2,3,4')->order('id desc')->select();
        if ($order) {
            $path = "/api/card/open/queryOrder";//url
            $key  = config('site.kgj_appkey');
            $r    = '';
            foreach ($order as $k => $v) {
                sleep(2);
//                $packageInfo = Packages::where(['id' => $v['goods_id']])->find();

                $data['outTradeNo'] = $v['outTradeNo'];
                $arr                = kgjcurl($path, $data, $key);
                if (isset($arr['code']) && $arr['code'] == 200 && !empty($arr['data'])) {
                    $arr = $arr['data'];
                    if ($arr['orderStatus'] == 'error' || $arr['orderStatus'] == 'intercept') {
                        $v->order_status = 0;
                    } elseif ($arr['orderStatus'] == 'abnormal' || $arr['orderStatus'] == 'wait_edit' || $arr['orderStatus'] == 'wait_again' || $arr['orderStatus'] == 'in_process' || $arr['orderStatus'] == 'under_review') {
                        $v->order_status = 3;
                    } elseif ($arr['orderStatus'] == 'no_shipped' || $arr['orderStatus'] == 'shipped') {
                        $v->order_status = 2;
                    } elseif ($arr['orderStatus'] == 'activate') {
                        $v->order_status = 1;
                    } elseif ($arr['orderStatus'] == 'first_flush') {
                        $v->order_status = 4;
                    }
                    if (!empty($arr['expressNum'])) {
                        $v->shipping_no = $arr['expressNum'];
                    }
                    if (!empty($arr['expressName'])) {
                        $v->shipping_company_name = $arr['expressName'];
                    }
                    if (!empty($arr['activatePhone']) && empty($v->phone)) {
                        $v->phone = $arr['activatePhone'];
                    }
//                    if (strpos($rows['remark'], '100') !== false || strpos($rows['remark'], '可结算')) {
//                        $v->order_status    = 5;
//                        $v->settlement_time = time();
//                        $userData           = findAncestors($v->user_id);
//                        if (!$userData) {
//                            //顶级代理下单
//                            dingji($v->user_id, $v->id, $v->goods_id, $packageInfo['rebate'], false, true);
//                        } else {
//                            array_unshift($userData, $v->user_id);
//                            $money = moneydigui($v->user_id, $v->goods_id);
//                            if ($money) {
//                                digui($v->user_id, $money, $v->goods_id, $v->id, 0, true);
//                            } else {
//                                //顶级
//                                dingji($userData[count($userData) - 1], $v['id'], $v->goods_id, $packageInfo['rebate'],
//                                    false, true);
//                            }
//                        }
//                    }
                    $v->order_info_json   = json_encode($arr, true);
                    $v->order_status_text = $arr['statusDesc'];
                    $v->remark            = $arr['statusDesc'];
                    $r                    = $v->save();
                    if ($r) {
                        echo "操作成功";
                    } else {
                        echo "更新失败";
                    }
                }
            }
        }

    }
}
